const express = require('express');

const mongodb =  require('mongodb');
const { MongoClient, ObjectId } = mongodb;

const dbUrl = 'mongodb://localhost:27017';

function getSingers(p) {
    return new Promise(function (resolve, reject) {
        MongoClient.connect(dbUrl, { useNewUrlParser: true },function (err, client) {
            if (err) {
                console.log(err);
                reject(err);
                return;
            }

            let myTestDB = client.db('MusicDB');
            let singersCollection = myTestDB.collection('singer');
            singersCollection.find({}).toArray(function (err, result) {
                if (err) {
                    console.log(err);
                    reject(err);
                    return;
                }
                client.close();
                resolve(result);
            });
        });
    });
}

function getSingerPage(pageNo, pageSize) {
    return new Promise(function (resolve, reject) {
        MongoClient.connect(dbUrl, { useNewUrlParser: true },function (err, client) {
            if (err) {
                console.log(err);
                reject(err);
                return;
            }

            let myTestDB = client.db('MusicDB');
            let singersCollection = myTestDB.collection('singer');
            singersCollection.find({}).skip(pageNo * pageSize).limit(pageSize).toArray(function (err, result) {
                if (err) {
                    console.log(err);
                    reject(err);
                    return;
                }
                client.close();
                setTimeout(function () {
                    resolve(result);
                }, 2000)

            });
        });
    });
}

function addSinger(singer) {
    return new Promise(function (resolve, reject) {
        MongoClient.connect(dbUrl, { useNewUrlParser: true },function (err, client) {
            if (err) {
                console.log(err);
                reject(err);
                return;
            }
            let myTestDB = client.db('MusicDB');
            let singersCollection = myTestDB.collection('singer');
            singersCollection.insertOne(singer, function (err, result) {
                if (err) {
                    console.log(err);
                    reject(err);
                    return;
                }

                singer._id = result.insertedId;

                resolve(singer)
            })
        });
    });
}

function delSinger(singer_id) {
    return new Promise(function (resolve, reject) {
        MongoClient.connect(dbUrl, { useNewUrlParser: true },function (err, client) {
            if (err) {
                console.log(err);
                reject(err);
                return;
            }
            let myTestDB = client.db('MusicDB');
            let singersCollection = myTestDB.collection('singer');
            singersCollection.removeOne({_id: ObjectId(singer_id)}, function (err, result) {
                if (err) {
                    console.log(err);
                    reject(err);
                    return;
                }

                let isOk = result.deletedCount === 1;
                resolve(isOk)

            })
        });
    });
}

function getSongBySingerId(singer_id) {
    return new Promise(function (resolve, reject) {
        MongoClient.connect(dbUrl, { useNewUrlParser: true },function (err, client) {
            if (err) {
                console.log(err);
                reject(err);
                return;
            }
            let myTestDB = client.db('MusicDB');
            let singersCollection = myTestDB.collection('Songs');
            singersCollection.find({singer_id: singer_id}).toArray(function (err, result) {
                if (err) {
                    console.log(err);
                    reject(err);
                    return;
                }

                setTimeout(function () {
                    resolve(result)
                }, 2000)
            });
        });
    });
}

const app=express()
const path=require("path")
const bodyParser = require('body-parser');

app.use(express.static(path.join(__dirname,"uploads")))
app.use(bodyParser.json())

app.get('/music/getSingers', async function(req,res){
    let singer=[]
    let status=10000
    let msg="ok"
    try{
     singer=await getSingers()
    }catch(e){
        status=10005
        msg=e.message
    }
    res.send({
        status,
        msg,
        data:singer
    })
})

app.get("/music/getSingerPage",async function(){
    let pageNo=Number(req.query.pageNO);
    let pageSize=Number(req.query.pageSize)

    let singer=[]
    let status=10000
    let msg="ok"
    try{
     singer=await getSingerPage(pageNo, pageSize)
    }catch(e){
        status=10005
        msg=e.message
    }
    res.send({
        status,
        msg,
        data:singer
    })
})


const port=5000
app.listen(port,function(err,res){
    if(err){
        return console.log(err)
    }
    console.log(`正在监听${port}端口`)
})
